var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var communityUrl = require("url/community");
var circleUrl = require("url/circle");
var communityUtil = require("helper/community");
var httpSilence = require("helper/httpSilence");
var hashdata = mm("do_HashData");
var qqHttp = require("helper/http");

//UI
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);

var listdata = mm("do_ListData");
var listView = ui("listView");
listView.bindItems(listdata);


laodPage();
var page,tagList,tabType=1;
var isLoadFirst=false;
function laodPage() {
	isLoadFirst=false;
	page = 1;
	var url = communityUrl.url.base.indexNew;
	var param = {
		"communityId" : communityUtil.communityVisitId()
	}
	httpSilence.post(url, param, function(data) {
		listView.rebound();
		listdata.removeAll();
		/* borderView标题赋值 */
		var dataTag = {};
		dataTag.name = data.communityList[0].name;
		if(data.communityList[0].txtShow){
			dataTag.text="未加入";
		}else{
			dataTag.text="";
		}
		hashdata.addData({
			"labelTitle" : dataTag.name+dataTag.text,
		})
		header.refreshData();
		communityUtil.communityVisitId(data.communityList[0].id);
		var noticeContent=[{"content":"暂无最新公告"}];
		var noticeTime="";
		if(null!=data.noticeList&&data.noticeList.length>0){
			noticeContent=data.noticeList;
		}
		var showCanJoin=false;
		var showCanSign=false;
		var showCanSigned=false;
		if(data.showCanJoinSign=="1"){//加入社区
			showCanJoin=true;
			showCanSign=false;
			showCanSigned=false;
		}else if(data.showCanJoinSign=="2"){//签到
			showCanJoin=false;
			showCanSign=true;
			showCanSigned=false;
		}else if(data.showCanJoinSign=="3"){//已签到
			showCanJoin=false;
			showCanSign=false;
			showCanSigned=true;
		}
		
		listdata.addData([{
			"template" : 0,
			"crollbannerList":data.crollbannerList,
			"communityName":data.communityList[0].name,
			"qShopCount":"商家: "+data.qShopCount,
			"circleCount":"圈子: "+data.circleCount,
			"neighborCount":"邻居: "+data.neighborCount,
			"noticeContent":noticeContent,
			//"noticeTime":noticeTime,
			"activityList":data.activityList,
			"circleBaseList":data.circleBaseList,
			"showCanJoin":showCanJoin,
			"showCanSign":showCanSign,
			"showCanSigned":showCanSigned
		}]);		
		tagList=data.tagList;
		listdata.addData([{
			"template" : 1,
			"communityMessageTags":tagList,
			"isCommunityMessage":false
		}]);
		if(tabType==1){
			getCommunityMessage();
		}else if(tabType==2){
			getCircle();
		}
		listView.refreshItems();
		do_Page.fire("community_update_isNewsLoaded");
	})
}
var noCommunityMessageData = [ {
	"template" : 5
} ];
var tagIds="";
function getCommunityMessage(){
	page=1;
	isLoadFirst=false;
	var messageUrl = communityUrl.url.message.newPage;
	var param = {
		"page" : page,
		"communityId" : communityUtil.communityVisitId(),
		"tagIds" : tagIds
	}
	httpSilence.post(messageUrl, param, function(data) {
		listView.rebound();
		var tabTypeData=listdata.getOne(1);
		tabTypeData.isCommunityMessage=true;
		listdata.updateOne(1, tabTypeData);
		if (data.page.content.length>0) {
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].template=2;
			}
			listdata.addData(data.page.content);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
		isLoadFirst=true;
	});
}
function getNextPageCommunityMessage(){
	page++;
	var messageUrl = communityUrl.url.message.newPage;
	var param = {
		"page" : page,
		"communityId" : communityUtil.communityVisitId(),
		"tagIds" : tagIds
	}
	qqHttp.post(messageUrl, param, function(data) {
		if (data.page.content.length>0) {
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].template=2;
			}
			listdata.addData(data.page.content);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
	});
}
function getCircle(){
	page=1;
	isLoadFirst=false;
	var circleTopUrl = circleUrl.url.topic.listCicleTopic4Community;
	var param = {
		"page" : page
		/*"communityId" : communityUtil.communityVisitId()*/
	}
	httpSilence.post(circleTopUrl, param, function(data) {
		listView.rebound();
		var tabTypeData=listdata.getOne(1);
		tabTypeData.isCommunityMessage=false;
		listdata.updateOne(1, tabTypeData);
		var activityBaseVo = data.activityBaseVo;
		if(activityBaseVo){
			activityBaseVo.template =3;//指定模板
			listdata.addOne(activityBaseVo);
		}
		if (data.topicPage&&data.topicPage.content.length>0) {
			for (var i = 0; i < data.topicPage.content.length; i++) {
				data.topicPage.content[i].template=4;
			}
			listdata.addData(data.topicPage.content);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
		isLoadFirst=true;
	});
}
function getNextPageCircle(){
	page++;
	var circleTopUrl = circleUrl.url.topic.listCicleTopic4Community;
	var param = {
		"page" : page
		/*"communityId" : communityUtil.communityVisitId()*/
	}
	qqHttp.post(circleTopUrl, param, function(data) {
		if (data.topicPage&&data.topicPage.content.length>0) {
			for (var i = 0; i < data.topicPage.content.length; i++) {
				data.topicPage.content[i].template=4;
			}
			listdata.addData(data.topicPage.content);
		} else {
			if(page==1){
				listdata.addData(noCommunityMessageData);
			}
		}
		listView.refreshItems();
	});
}
//下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (isLoadFirst&&data.state == 2) {
		if(tabType==1){
			getNextPageCommunityMessage();
		}else if(tabType==2){
			getNextPageCircle();
		}
		listView.rebound();
	}
});
var colors=["949EAAFF","ff5305FF"];
do_Page.on("community_newIndex_tab", function(data) {
	if(null!=data&&data.tabType=="circle"){
		changeTabType(2,null);
		getCircle();
	}else if(null!=data&&data.tabType=="communityMessage"){
		changeTabType(1,data.tagIndex);
		getCommunityMessage();
	}
});
function changeTabType(tab,tagIndex){
	tabType=tab;
	var tabTypeData=listdata.getOne(1);
	if(null!=tagIndex){//更新邻里动态tag的颜色
		for (var i = 0; i < tagList.length; i++) {
			if(i==tagIndex){
				if(tagList[i].isCheck){
					tagIds="";
					tagList[i].tagFontColor=colors[0];
					tagList[i].isCheck=false;
				}else{
					tagIds=tagList[i].id;
					tagList[i].tagFontColor=colors[1];
					tagList[i].isCheck=true;
				}
			}else{
				tagList[i].tagFontColor=colors[0];
				tagList[i].isCheck=false;
			}
		}
		tabTypeData.communityMessageTags=tagList;
		listdata.updateOne(1, tabTypeData);
	}
	listdata.removeRange(2, listdata.getCount()-1);
}
//index点击社区
do_Page.on("index_community_tab_touch", function(data) {
	laodPage();
});
//点击加入社区或者签到
do_Page.on("community_join_sign", function(data) {
	var topData=listdata.getOne(0);
	topData.showCanJoin=false;
	topData.showCanSign=false;
	topData.showCanSigned=true;
	listdata.updateOne(0, topData);
	listView.refreshItems();
});




